const express = require('express');
const path = require('path');//A utility module that deals with paths.
const joi = require('joi');//
const bodyParser = require('body-parser');
const app = express();//express contains a bunch of methods;

//1.
app.use('/',express.static(path.join(__dirname,'/9.Static Folder')));

//2.allowing us to parse URL encoded forms; The extended option is set to false because we aren't dealing with any complicated objects.
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
//3.加载页面
app.get('/',(req,res)=>{
    res.sendFile(path.join(__dirname,'/9.Static Folder','16.index.html'));
});
//4.post
app.post('/',(req,res)=>{
    console.log(req.body);
    const schema = joi.object().keys({
        email:joi.string().trim().email().required(),
        password:joi.string().min(5).max(10).required()
    });
    joi.validate(req.body,schema,(err,result)=>{
        if(err){
            console.log(err);
            res.send('an error has occurred');
        }
        console.log(result);
        res.send('successfuly posted data');
    });
});

app.listen(3000);